﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

namespace TimKen.DataAccess
{
    /// <summary>
    /// 数据访问类TransTypeDAO。
    /// </summary>
    public class TransTypeDAO
    {
        public TransTypeDAO()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int TransTypeID, int LanguageID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@TransTypeID", SqlDbType.Int,4),
					new SqlParameter("@LanguageID", SqlDbType.Int,4)};
            parameters[0].Value = TransTypeID;
            parameters[1].Value = LanguageID;

            int result = DbHelperSQL.RunProcedure("UP_TransType_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("TransTypeID", "TransType");
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public int Add(TimKen.DataEntity.TransType model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@TransTypeID", SqlDbType.Int,4),
					new SqlParameter("@TransTypeName", SqlDbType.VarChar,50),
					new SqlParameter("@LanguageID", SqlDbType.Int,4)};
            parameters[0].Value = model.TransTypeID;
            parameters[1].Value = model.TransTypeName;
            parameters[2].Value = model.LanguageID;

            DbHelperSQL.RunProcedure("UP_TransType_ADD", parameters, out rowsAffected);
            return (int)parameters[0].Value;
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.TransType model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@TransTypeID", SqlDbType.Int,4),
					new SqlParameter("@TransTypeName", SqlDbType.VarChar,50),
					new SqlParameter("@LanguageID", SqlDbType.Int,4)};
            parameters[0].Value = model.TransTypeID;
            parameters[1].Value = model.TransTypeName;
            parameters[2].Value = model.LanguageID;

            DbHelperSQL.RunProcedure("UP_TransType_Update", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /*
        public void Delete(int TransTypeID, int LanguageID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@TransTypeID", SqlDbType.Int,4),
					new SqlParameter("@LanguageID", SqlDbType.Int,4)};
            parameters[0].Value = TransTypeID;
            parameters[1].Value = LanguageID;

            DbHelperSQL.RunProcedure("UP_TransType_Delete", parameters, out rowsAffected);
        }
        */

        public void Delete(int TransTypeID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@TransTypeID", SqlDbType.Int,4)};
            parameters[0].Value = TransTypeID;

            DbHelperSQL.RunProcedure("UP_TransType_Delete", parameters, out rowsAffected);
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.TransType GetModel(int TransTypeID, int LanguageID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@TransTypeID", SqlDbType.Int,4),
					new SqlParameter("@LanguageID", SqlDbType.Int,4)};
            parameters[0].Value = TransTypeID;
            parameters[1].Value = LanguageID;

            TimKen.DataEntity.TransType model = new TimKen.DataEntity.TransType();
            DataSet ds = DbHelperSQL.RunProcedure("UP_TransType_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["TransTypeID"].ToString() != "")
                {
                    model.TransTypeID = int.Parse(ds.Tables[0].Rows[0]["TransTypeID"].ToString());
                }
                model.TransTypeName = ds.Tables[0].Rows[0]["TransTypeName"].ToString();
                if (ds.Tables[0].Rows[0]["LanguageID"].ToString() != "")
                {
                    model.LanguageID = int.Parse(ds.Tables[0].Rows[0]["LanguageID"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TransTypeID,TransTypeName,LanguageID ");
            strSql.Append(" FROM TransType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "TransType";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        /// <summary>
        /// 获取满足条件的数据数量
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns></returns>
        public long GetListCount(string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "TransType";
            parameters[1].Value = "TransTypeID";
            parameters[2].Value = 1;
            parameters[3].Value = 0;
            parameters[4].Value = strWhere;
            SqlDataReader dr = DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters);
            if (dr.Read())
            {
                long count  = Convert.ToInt64(dr[0]);

               dr.Close();
                dr.Dispose();

              return count;
            }
            return 0;

        }

        #endregion  成员方法
    }
}

